<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\wechat_zhl\controller; //Demo插件英文名，改成你的插件英文就行了

use cmf\controller\PluginAdminBaseController;
use think\Request;

class AdminWechatController extends PluginAdminBaseController
{

    /**
     * 添加微信公众号
     * @adminMenu(
     *     'name'   => '添加微信公众号',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加微信公众号',
     *     'param'  => ''
     * )
     */
    public function add()
    {

        return $this->fetch();
    }

    /**
     * 添加微信公众号提交保存
     * @adminMenu(
     *     'name'   => '添加微信公众号提交保存',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加微信公众号提交保存',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data = $this->request->param();

        $result = $this->validate($data, "AdminWechat");

        if ($result !== true) {
            $this->error($result);
        }

        $WechatSettings = cmf_get_option('wechat_settings');

        if (!empty($data['is_default'])) {
            $WechatSettings['default'] = $data;
        }

        unset($data['is_default']);

        $WechatSettings['wechats'][$data['app_id']] = $data;

        cmf_set_option('wechat_settings', $WechatSettings);

        $this->success('添加成功！', cmf_plugin_url('WechatZhl://AdminWechat/edit', ['id' => $data['app_id']]));


    }

    /**
     * 编辑微信公众号
     * @adminMenu(
     *     'name'   => '编辑微信公众号',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑微信公众号',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $appId = $this->request->param('id');
        $request = Request::instance();
        $domain=$request->domain();
        $WechatSettings = cmf_get_option('wechat_settings');

        if (!empty($WechatSettings['wechats'][$appId])) {
            $this->assign('wechat', $WechatSettings['wechats'][$appId]);
        }

        $defaultWechat = empty($WechatSettings['default']) ? [] : $WechatSettings['default'];
        $this->assign('default_Wechat', $defaultWechat);
        $this->assign('domain', $domain);

        return $this->fetch();
    }

    /**
     * 编辑微信公众号提交保存
     * @adminMenu(
     *     'name'   => '编辑微信公众号提交保存',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑微信公众号',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

        $result = $this->validate($data, "AdminWechat");

        if ($result !== true) {
            $this->error($result);
        }

        $WechatSettings = cmf_get_option('wechat_settings');

        if (!empty($data['is_default'])) {
            $WechatSettings['default'] = $data;
        }

        unset($data['is_default']);

        $WechatSettings['wechats'][$data['app_id']] = $data;

        cmf_set_option('wechat_settings', $WechatSettings);

        $this->success('保存成功！');
    }

    /**
     * 删除微信公众号
     * @adminMenu(
     *     'name'   => '删除微信公众号',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除微信公众号',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $appId = $this->request->param('id');

        $WechatSettings = cmf_get_option('Wechat_settings');

        $defaultWechat = empty($WechatSettings['default']) ? [] : $WechatSettings['default'];

        if (!empty($defaultWechat['app_id']) && $appId == $defaultWechat['app_id']) {
            $this->error(' 默认微信公众号无法删除！');
        }

        unset($WechatSettings['Wechats'][$appId]);

        cmf_set_option('Wechat_settings', $WechatSettings);

        $this->success('删除成功！');
    }

}
